package edu.cth.stud.accelerateit.view.android;

import java.beans.PropertyChangeEvent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.cth.stud.accelerateit.R;
import edu.cth.stud.accelerateit.view.GUIController;

public class WordFinderActivity extends Activity implements
		IAndroidGUIComponent {

	private LinearLayout mainMenuScreen;
	private Button cawButton;
	private Button fawButton;
	private Button tutorialButton;
	private Button optionsButton;

	private TextView titleText;
	private TextView bottomText;
	
	
	private static String PREFS_NAME = "WordFinderSettings";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Moved code to the linkContent method for structure
		linkContent();

		// Load in settings made in options
		loadSettings();
		
		
		// Register this activity/view with the GUIController:
		GUIController.getInstance().registerListener(this);
	}
	
	private void loadSettings(){
		
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

		mainMenuScreen.setBackgroundColor(settings.getInt("backgroundColor", Color.BLACK));
		titleText.setTextColor(settings.getInt("textColor", Color.WHITE));
		bottomText.setTextColor(settings.getInt("textColor", Color.WHITE));
	}
	private void linkContent() {
		// Set the content view to main_menu.xml:
		setContentView(R.layout.main_menu);

		// Find the buttons:
		this.cawButton = (Button) this.findViewById(R.id.cawButton);
		this.fawButton = (Button) this.findViewById(R.id.fawButton);
		this.tutorialButton = (Button) this.findViewById(R.id.tutorialButton);
		this.optionsButton = (Button) this.findViewById(R.id.optionsButton);

		// Find the screen:
		mainMenuScreen = (LinearLayout) this.findViewById(R.id.mainMenuScreen);
		
		// Find the titleText:
		this.titleText = (TextView) this.findViewById(R.id.mainMenuTitle);
		
		// Find the bottomText
		this.bottomText = (TextView) this.findViewById(R.id.bottomText);

		// Connect listeners:
		this.cawButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				swapScreens(CorrectAWordActivity.class);
			}
		});

		this.fawButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				swapScreens(FindAWordActivity.class);

			}
		});
		this.tutorialButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				swapScreens(TutorialActivity.class);
			}
		});

		this.optionsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				swapScreens(OptionsActivity.class);
			}
		});
	}

	@Override
	public void onBackPressed() {
		// If back is pressed from the main menu, the application is to be left
		// (TODO: Fill in the requirement id for that)
		GUIController.getInstance().terminateApplication();
	}

	private void swapScreens(Class target) {
		if (target.getInterfaces()[0] == IAndroidGUIComponent.class) {
			
			// Required to keep down the activity count:
			finish();
			GUIController.getInstance().unregisterListener(this);
			
			// Get the context, create an intent:
			Context ourContext = this.getApplicationContext();
			Intent ourIntent = new Intent(ourContext, target);

			// Start the intended activity:nhjh
			startActivity(ourIntent);
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (event.getPropertyName().equals("Shutdown")) {
			GUIController.getInstance().unregisterListener(this);
			finish();
		}
	}
}